package com.androidified.personalshopper.ui;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidified.personalshopper.CommerceProvider;
import com.androidified.personalshopper.R;
import com.androidified.personalshopper.bean.Address;
import com.androidified.personalshopper.bean.Commerce;
import com.androidified.personalshopper.bean.Discount;
import com.androidified.personalshopper.bean.Promotion;

public class DetailActivity extends Activity {

	public static final String EXTRA_PROMO_ID = "promoId";
	public static final String EXTRA_COMMERCE_ID = "commerceId";
	
	private static final int[] IMAGES_IDS = {
		R.drawable.offer1,
		R.drawable.offer2 };
	
	private Promotion promotion;
	private Commerce commerce;
	
	private View lkxa;
	private ImageView promotion_image;
	private TextView promotion_text;
	private TextView promotion_description;
	private TextView promotion_address;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getActionBar().setDisplayShowHomeEnabled(false);
        
		setContentView(R.layout.detail_activity);
		findViews();
		
		final String commerceId = getIntent().getStringExtra(EXTRA_COMMERCE_ID);
		final String promoId = getIntent().getStringExtra(EXTRA_PROMO_ID);
		
		if (commerceId == null || promoId == null) {
			finish();
			return;
		} else {
			List<Commerce> commerces = CommerceProvider.get();
			for (Commerce commerce : commerces) {
				if (commerceId.equals(commerce.getId())) {
					this.commerce = commerce;
					List<Promotion> promos = commerce.getPromotions();
					for (Promotion promo : promos) {
						if (promoId.equals(promo.getId())) {
							this.promotion = promo;
							break;
						}
					}
					break;
				}
			}
		
			getActionBar().setTitle(commerce.getPublicName());
			
			final int codeLkxa = promotion.getId().codePointAt(promotion.getId().length() - 2);
			lkxa.setVisibility((codeLkxa % 2 == 1) ? View.VISIBLE : View.GONE);
			
			final int code = promotion.getId().codePointAt(promotion.getId().length() - 1);
			promotion_image.setImageResource(IMAGES_IDS[code % IMAGES_IDS.length]);
			
			if (Discount.ABSOLUTE.equals(promotion.getDiscount().getDiscountType())) {
				promotion_text.setText(promotion.getDiscount().getValue() + getString(R.string.promotion_absolute_short_text));
			} else if (Discount.PERCENTAGE.equals(promotion.getDiscount().getDiscountType())) {
				promotion_text.setText(promotion.getDiscount().getValue() + getString(R.string.promotion_porcentage_short_text));
			}
			
			promotion_description.setText(promotion.getDescription());
			
			Address addr = commerce.getAddress();
			if (addr == null) {
				// Fake info, we won't allow the app to crash in the demo.
				addr = new Address();
				addr.setStreet("Av. Diagonal");
				addr.setNumber("200");
				addr.setCity("Barcelona");
			}
			promotion_address.setText(addr.getStreet() + ", " + addr.getNumber() + ", " + addr.getCity());
		}
	}
	
	private void findViews() {
		promotion_image = (ImageView) findViewById(R.id.promotion_image);
		promotion_text = (TextView) findViewById(R.id.promotion_text);
		promotion_description = (TextView) findViewById(R.id.promotion_description);
		promotion_address = (TextView) findViewById(R.id.promotion_address);
		lkxa = findViewById(R.id.lkxa);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.detail_activity_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
}
